﻿@page "/features/icons"
@using System.Reflection

<DocsPage MaxWidth="MaxWidth.Large">
    <DocsPageHeader Title="MudBlazor Icons" SubTitle="Over 1800 Material Design icons and a few custom ones.">
        <Description>To use these icons, or if you want to load your own icons, you can check out the <MudLink Href="/components/icons">component's Icon Page</MudLink>.</Description>
    </DocsPageHeader>
    <DocsPageContent>
        <DocsPageSection>
            <SectionHeader Title="Custom Icons">
            </SectionHeader>
            <MudDivider />
            <SectionContent FullWidth="true">
                @if (MudIconsMaterial != null)
                {
                    <DocsIcons List="MudIconsCustom" />
                }
                else
                {
                    <MudText Typo="Typo.h5" Align="Align.Center">Loading...</MudText>
                }
            </SectionContent>
        </DocsPageSection>
        <DocsPageSection>
            <SectionHeader Title="Material Icon">
            </SectionHeader>
            <SectionContent Class="docs-icon-material-container" FullWidth="true">
                <MudRadioGroup T="string" SelectedOption="@SelectedIconType" SelectedOptionChanged="HandleSelectedOptionChanged">
                    @foreach (var item in IconTypes)
                    {
                        <MudRadio Option="@item.Key" Color="Color.Primary">@item.Key</MudRadio>
                    }
                </MudRadioGroup>
                <MudTextField Immediate="true" DebounceInterval="1000" @bind-Value="SearchText" Variant="Variant.Outlined" Placeholder="Search icons..." Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" />
                <MudText Class="mx-3" Typo="Typo.body1">@SelectedIcons.Count() matching results</MudText>
                <MudDivider />
                @if (MudIconsMaterial != null)
                {
                    <DocsIcons List="@SelectedIcons" />
                }
                else
                {
                    <MudText Typo="Typo.h5" Align="Align.Center">Loading...</MudText>
                }
            </SectionContent>
        </DocsPageSection>
    </DocsPageContent>
</DocsPage>

@code {

    List<MudIcons> MudIconsMaterial;
    List<MudIcons> MudIconsCustom;

    private string SelectedIconType { get; set; } = IconType.Filled;
    private string SearchText { get; set; } = string.Empty;
    private List<MudIcons> SelectedIcons => string.IsNullOrWhiteSpace(SearchText)
        ? MudIconsMaterial
        : MudIconsMaterial.Where(m => m.Name.Contains(SearchText, StringComparison.OrdinalIgnoreCase)).ToList();
    private readonly IDictionary<string, object> IconTypes = new Dictionary<string, object>()
    {
        { IconType.Filled,new Filled()},
        { IconType.Outlined, new Outlined()},
        { IconType.Rounded, new Rounded()},
        { IconType.Sharp, new Sharp()},
        { IconType.TwoTone, new TwoTone()},
    };

    protected override async Task OnInitializedAsync()
    {
        await LoadMaterialIcons(SelectedIconType);
        await LoadCustomIcons();
    }

    public async Task LoadMaterialIcons(string selectedIcoType)
    {
        var result = new List<MudIcons>();
        var icons = IconTypes[selectedIcoType];
        foreach (var prop in icons.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty ))
        {
            result.Add(new MudIcons(prop.Name, prop.GetValue(icons).ToString()));
        }
        await Task.WhenAll();
        MudIconsMaterial = result;
    }

    public async Task LoadCustomIcons()
    {
        var result = new List<MudIcons>();
        var brands = new Brands();
        foreach (var prop in typeof(Brands).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty))
            result.Add(new MudIcons(prop.Name, prop.GetValue(brands).ToString()));
        var fileFormats = new FileFormats();
        foreach (var prop in typeof(FileFormats).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty))
            result.Add(new MudIcons(prop.Name, prop.GetValue(fileFormats).ToString()));
        var uncategorized = new Uncategorized();
        foreach (var prop in typeof(Uncategorized).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty))
            result.Add(new MudIcons(prop.Name, prop.GetValue(uncategorized).ToString()));
        await Task.WhenAll();
        MudIconsCustom = result;
    }

    public async Task HandleSelectedOptionChanged(string e)
    {
        SelectedIconType = e;
        await LoadMaterialIcons(SelectedIconType);
    }

    private struct IconType
    {
        public const string Filled = "Filled",
                            Outlined = "Outlined",
                            Rounded = "Rounded",
                            Sharp = "Sharp",
                            TwoTone = "TwoTone";
    }
}